﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using Helpers;
using System.ComponentModel.Composition;
using System.Net;
using Google.Apis.Books.v1;
using DataModel;
using Google.Apis.Books.v1.Data;

namespace SearchByGoogle
{
    [Export(typeof(IBookSearch))]
    [ExportMetadata("Rank", 2)]
    [ExportMetadata("Name", "Google")]
    public class BookSearch : IBookSearch
    {
        public BookSearch()
        {
            Logger.Write("BookSearch()");
        }

        public List<DataModel.Book> Search(string ISBN, string Title)
        {
            List<string> Query = new List<string>();

            Query.Add(Title);
            if (ISBN != null && ISBN != "")
                Query.Add("isbn:" + ISBN);
            string q = string.Join("+", Query);

            var s = new BooksService();
            var v = s.Volumes.List(q).Fetch();
            if (v.TotalItems > 0)
            {
                List<DataModel.Book> t = new List<DataModel.Book>();
                foreach (var x in v.Items)
                {
                    Book b = new Book
                    {
                        ISBN = GetISBN(x),
                        Title = x.VolumeInfo.Title,
                        Authors = x.VolumeInfo.Authors != null ? x.VolumeInfo.Authors.Select(y => new Author { Name = y }).ToList() : null,
                        NumberOfPages = (int?)x.VolumeInfo.PageCount,
                        Publisher = new Publisher { Name = x.VolumeInfo.Publisher },
                        PublicationDate = x.VolumeInfo.PublishedDate,
                        LanguagePublished = x.VolumeInfo.Language,
                        DetailPageUrl = x.VolumeInfo.PreviewLink,
                        Image = GetImage(x.VolumeInfo.ImageLinks)
                    };
                    t.Add(b);
                }
                return t;
            }
            return null;
        }

        public string GetImage(Volume.VolumeInfoData.ImageLinksData images)
        {
            if (images == null)
                return "";

            if (images.Large != null && images.Large != "")
                return images.Large;
            if (images.Medium != null && images.Medium != "")
                return images.Medium;
            if (images.Small != null && images.Small != "")
                return images.Small;
            if (images.Thumbnail != null && images.Thumbnail != "")
                return images.Thumbnail;
            if (images.SmallThumbnail != null && images.SmallThumbnail != "")
                return images.SmallThumbnail;

            return "";
        }

        public string GetISBN(Volume v)
        {
            var q = v.VolumeInfo.IndustryIdentifiers.Where(x => x.Type.StartsWith("ISBN")).FirstOrDefault();
            if (q != null)
                return q.Identifier;
            return null;
        }
    }

}
